package com.zfsbs.activity.test;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Log;

public class FileSaveService {
	/**
	 * 保存数据
	 * 
	 * @param outputStream
	 * @param content
	 * @throws Exception
	 */
	public static void save(OutputStream outputStream, String content)
			throws Exception {
		outputStream.write(content.getBytes());
		outputStream.close();
	}

	public static void saveBitmap(Bitmap bitmap) {

		File f = new File(Environment.getExternalStorageDirectory(), "print");
		Log.i("TAG", "文件路径："+f.getAbsolutePath());
		if (f.exists()) {
			f.delete();
		}
		try {
			FileOutputStream out = new FileOutputStream(f);
			bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
			out.flush();
			out.close();
			Log.i("TAG", "已经保存");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 读取数据
	 * 
	 * @param inputStream
	 * @return
	 * @throws Exception
	 */
	public static String read(InputStream inputStream) throws Exception {
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); // 往内存写数据
		byte[] buffer = new byte[1024]; // 缓冲区
		int len = -1;
		while ((len = inputStream.read(buffer)) != -1) {
			byteArrayOutputStream.write(buffer, 0, len);
		}
		byte[] data = byteArrayOutputStream.toByteArray(); // 存储数据
		byteArrayOutputStream.close();
		inputStream.close();
		return new String(data);
	}
}
